
/**
 * Stack.
 * @param <T> Generic type.
 * @author Lukas Koell
 * @author David Medenjak
 * @author Johannes Riedmann
 */
public class Stack<T> implements Container<T> {

  /**
   * Reference to the last item.
   */
  private LinkedItem<T> last;

  /**
   * Adds an item to the container at last.
   * @param item Item to add.
   */
  public void push(T item) {
    if (last == null) {
      last = new LinkedItem<T>(item);
    } else {
      LinkedItem<T> newItem = new LinkedItem<T>(item);
      newItem.setPrevious(last);
      last = newItem;
    }
  }

  /**
   * Gets the last element from the container and removes it.
   * @return Next element.
   */
  public T pop() {
    LinkedItem<T> element = last;
    last = last.getPrevious();
    return element.getValue();
  }

  /**
   * Gets the last element from the container.
   * @return Next element.
   */
  public T peek() {
    if (last != null) {
      return last.getValue();
    } else {
      return null;
    }
  }

  /**
   * Checks if an item is left.
   * @return True if it's empty, false if it's not.
   */
  public boolean isEmpty() {
    if (last == null) {
      return true;
    }
    return false;
  }
}
